package com.gqj.bas.controller;

import com.gqj.bas.domain.BasCabinetGrid;
import com.gqj.bas.service.IBasCabinetGridService;
import com.gqj.common.annotation.Log;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.enums.BusinessType;
import com.gqj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 层格Controller
 * 
 * @author lxq
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/bas/basGrid")
public class BasCabinetGridController extends BaseController
{
    @Autowired
    private IBasCabinetGridService basCabinetGridService;

    /**
     * 查询层格列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:list')")
    @GetMapping("/list")
    public AjaxResult list(BasCabinetGrid basCabinetGrid)
    {
        basCabinetGrid.setDelFlag("0");
        List<BasCabinetGrid> list = basCabinetGridService.selectBasCabinetGridList(basCabinetGrid);
        return AjaxResult.success(list);
    }

    /**
     * 查询柜架列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:gridOptions')")
    @GetMapping("/gridOptions")
    public AjaxResult gridOptions(BasCabinetGrid basCabinetGrid)
    {
        List<Map<String, Object>> list = basCabinetGridService.gridOptions(basCabinetGrid);
        return AjaxResult.success(list);
    }

    /**
     * 导出层格列表
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:export')")
    @Log(title = "层格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasCabinetGrid basCabinetGrid)
    {
        List<BasCabinetGrid> list = basCabinetGridService.selectBasCabinetGridList(basCabinetGrid);
        ExcelUtil<BasCabinetGrid> util = new ExcelUtil<BasCabinetGrid>(BasCabinetGrid.class);
        util.exportExcel(response, list, "层格数据");
    }

    /**
     * 获取层格详细信息
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:query')")
    @GetMapping(value = "/{gridId}")
    public AjaxResult getInfo(@PathVariable("gridId") Long gridId)
    {
        return AjaxResult.success(basCabinetGridService.selectBasCabinetGridByGridId(gridId));
    }

    /**
     * 新增层格
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:add')")
    @Log(title = "层格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasCabinetGrid basCabinetGrid)
    {
        basCabinetGrid.setCreateBy(getUsername());
        return toAjax(basCabinetGridService.insertBasCabinetGrid(basCabinetGrid));
    }

    /**
     * 修改层格
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:edit')")
    @Log(title = "层格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasCabinetGrid basCabinetGrid)
    {
        basCabinetGrid.setUpdateBy(getUsername());
        return toAjax(basCabinetGridService.updateBasCabinetGrid(basCabinetGrid));
    }

    /**
     * 删除层格
     */
    @PreAuthorize("@ss.hasPermi('bas:basGrid:remove')")
    @Log(title = "层格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gridIds}")
    public AjaxResult remove(@PathVariable Long[] gridIds)
    {
        for(Long gridId: gridIds){
            if (basCabinetGridService.hasChildByGridId(gridId))
            {
                return AjaxResult.error("存在下级层格,不允许删除");
            }
            if (basCabinetGridService.checkMaterialExistGrid(gridId))
            {
                return AjaxResult.error("层格已被引用,不允许删除");
            }
        }
        return toAjax(basCabinetGridService.deleteBasCabinetGridByGridIds(gridIds));
    }
}
